package com.elmcode.server.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Configuration
@Slf4j
public class RabbitMQConfig {

    public static final String ORDER_EVENT_EXCHANGE = "order-event-exchange";

    public static final String ORDER_DELAY_QUEUE = "order.delay.queue";

    public static final String ORDER_RELEASE_ORDER_QUEUE = "order.release.order.queue";

    public static final String EXCHANGE_TO_DELAY_QUEUE_KEY = "order.create.order";

    public static final String EXCHANGE_TO_RELEASE_QUEUE_KEY = "order.release.order";


    @Autowired
    RabbitTemplate rabbitTemplate;


    /**
     * 定制RabbitTemplate
     */
    @PostConstruct
    public void initRabbitTemplate(){
        //设置确认回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             *
             * @param correlationData 当前消息的唯一关联数据（这个是消息的唯一id）
             * @param ack 消息是否成功收到
             * @param cause 失败的原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                log.info("confirm...correlationData[{}]===>ack[{}]===>cause[{}]",correlationData,ack,cause);
            }
        });
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


    @Bean
    public Exchange createOrderExchange(){
        return new TopicExchange(ORDER_EVENT_EXCHANGE,true,false);
    }

    @Bean
    public Queue createOrderDelayQueue(){
        HashMap<String, Object> map = new HashMap<>();
        /**
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key: order.release.order
         * x-message-ttl: 60000
         */
        map.put("x-dead-letter-exchange",ORDER_EVENT_EXCHANGE);
        map.put("x-dead-letter-routing-key",EXCHANGE_TO_RELEASE_QUEUE_KEY);
        map.put("x-message-ttl",60000 * 5);
        return new Queue(ORDER_DELAY_QUEUE,true,false,false,map);
    }


    @Bean
    public Queue createOrderReleaseQueue(){
        return new Queue(ORDER_RELEASE_ORDER_QUEUE,true,false,false);
    }


    @Bean
    public Binding orderCreateOrderBinding(){
        /**
         * String destination, DestinationType destinationType,
         * String exchange, String routingKey, @Nullable Map<String, Object> arguments
         */
        return new Binding(ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE,
                EXCHANGE_TO_DELAY_QUEUE_KEY,
                null);
    }

    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding(ORDER_RELEASE_ORDER_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE,
                EXCHANGE_TO_RELEASE_QUEUE_KEY,
                null);
    }


}
